package com.sendpo.zjx.sendpo.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.activity.UserActiveInfoActvitiy;
import com.sendpo.zjx.sendpo.domain.BibiliNvgDomain;
import com.sendpo.zjx.sendpo.domain.FriendCircleDomain;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;
import com.sendpo.zjx.sendpo.views.AutoScrollViewPager;
import com.sendpo.zjx.sendpo.views.MainViewpager;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zjx on 2018
 */

public class YanchuFragment extends BaseMainFragment implements AMapLocationListener {
    @BindView(R.id.index_main_imgviewpager)
    AutoScrollViewPager indexMainImgviewpager;
    @BindView(R.id.dot1)
    View dot1;
    @BindView(R.id.dot2)
    View dot2;
    @BindView(R.id.dot3)
    View dot3;

    @BindView(R.id.saishi_iv)
    ImageView saishiIv;
    @BindView(R.id.saishi_tv)
    TextView saishiTv;
    @BindView(R.id.yanchu_saishibt)
    RelativeLayout yanchuSaishibt;
    @BindView(R.id.yanchu_iv)
    ImageView yanchuIv;
    @BindView(R.id.yanchu_tv)
    TextView yanchuTv;
    @BindView(R.id.yanchu_yanchubt)
    RelativeLayout yanchuYanchubt;
    @BindView(R.id.jianshen_iv)
    ImageView jianshenIv;
    @BindView(R.id.jianshen_tv)
    TextView jianshenTv;
    @BindView(R.id.yanchu_jianshenbt)
    RelativeLayout yanchuJianshenbt;
    @BindView(R.id.jiangzuo_iv)
    ImageView jiangzuoIv;
    @BindView(R.id.jiangzuo_tv)
    TextView jiangzuoTv;
    @BindView(R.id.yanchu_jiangzuobt)
    RelativeLayout yanchuJiangzuobt;
    @BindView(R.id.zhanlan_iv)
    ImageView zhanlanIv;
    @BindView(R.id.zhanlan_tv)
    TextView zhanlanTv;
    @BindView(R.id.yanchu_zhanlanbt)
    RelativeLayout yanchuZhanlanbt;
    @BindView(R.id.cangge_iv)
    ImageView canggeIv;
    @BindView(R.id.cangge_tv)
    TextView canggeTv;
    @BindView(R.id.yanchu_canggebt)
    RelativeLayout yanchuCanggebt;
    @BindView(R.id.gongyi_iv)
    ImageView gongyiIv;
    @BindView(R.id.gongyi_tv)
    TextView gongyiTv;
    @BindView(R.id.yanchu_gongyibt)
    RelativeLayout yanchuGongyibt;
    @BindView(R.id.mainviewpager)
    MainViewpager mainviewpager;

    private View rootView;
    public static final int[] imgs_dot = new int[]{R.id.dot1, R.id.dot2, R.id.dot3};

    public View initView() {
        View localView = View.inflate(this.mActivity, R.layout.fragment_yanchu, null);
        ButterKnife.bind(localView, this.mActivity);
        return localView;
    }

    public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
        if (this.rootView == null) {
            this.rootView = super.onCreateView(paramLayoutInflater, paramViewGroup, paramBundle);
            ButterKnife.bind(this, this.rootView);
            initData();
        }

        return this.rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        nvg();
    }

    private void initData() {
      List<BaseMainFragment> fragments=new ArrayList<>();
       for(int i=0;i<7;i++){
           fragments.add(new YanchuFragment_Saishi(i));
       }
         mainviewpager.setAdapter(new MainViewPagerAdapter(getChildFragmentManager(),fragments));
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {

    }

    public AMapLocationClientOption getDefaultAmapLocationOptions() {
        AMapLocationClientOption localAMapLocationClientOption = null;
        if (0 == 0) {
            localAMapLocationClientOption = new AMapLocationClientOption();
            localAMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
            localAMapLocationClientOption.setLocationCacheEnable(true);
            localAMapLocationClientOption.setOnceLocationLatest(false);
            localAMapLocationClientOption.setNeedAddress(true);
            localAMapLocationClientOption.setWifiScan(true);
            localAMapLocationClientOption.setInterval(2000L);
            localAMapLocationClientOption.setHttpTimeOut(20000L);
        }
        return localAMapLocationClientOption;
    }

    //商品导航条
    public void nvg() {
        Map<String, String> map = new HashMap<>();
        map.put("page", "1");
        map.put("pageSize", "3");
        map.put("city", "" + mActivity.userCity);
        Xutils.GET(HttpUrl.merchantsActivitiegetBilling, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                BibiliNvgDomain cache_domain = gson.fromJson(result, BibiliNvgDomain.class);
                if (cache_domain.getFlag()) {
                    if (cache_domain.getData() != null && cache_domain.getData().size() >= 3) {
                        Tools.setCache(mActivity, HttpUrl.merchantsActivitiegetBilling, result);
                        List<ImageView> imageViews = new ArrayList<>();
                        for (int i = 0; i < 3; i++) {
                            final BibiliNvgDomain.Data dataBean = cache_domain.getData().get(i);
                            ImageView iv = new ImageView(mActivity);
                            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                            imageViews.add(iv);
                            Glide.with(mActivity)
                                    .load("" + dataBean.getImg())
                                    .asBitmap()
                                    .placeholder(getResources().getColor(R.color.default_lightgray))
                                    .animate(R.anim.fade_in)
                                    .into(iv);
                            iv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Map<String, Object> map = new HashMap<>();
                                    FriendCircleDomain.DataBean dataBean1 = new FriendCircleDomain.DataBean();
                                    dataBean1.setUserFriendscircleId(dataBean.getId());
                                    map.put("data",dataBean1);
                                    Tools.redirectActivity(YanchuFragment.this.mActivity, UserActiveInfoActvitiy.class, map);
                                }
                            });

                        }
                        indexMainImgviewpager.setAdapter(new ImgBannerViewpagerAdpater(imageViews));
                        indexMainImgviewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                                for (int i = 0; i < imgs_dot.length; i++) {
                                    View iv = rootView.findViewById(imgs_dot[i]);
                                    if (iv != null) {
                                        if (i == position) {
                                            iv.setAlpha(1.0f);
                                        } else {
                                            iv.setAlpha(0.4f);
                                        }
                                    }
                                }
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        });
                        indexMainImgviewpager.setCurrentItem(0, true);
                        indexMainImgviewpager.setDirection(AutoScrollViewPager.RIGHT);
                        indexMainImgviewpager.setInterval(3000);
                        indexMainImgviewpager.setAutoScrollDurationFactor(10);
                        indexMainImgviewpager.startAutoScroll(3000);
                    }


                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    private class ImgBannerViewpagerAdpater extends PagerAdapter {
        public List<ImageView> imageViews;

        public ImgBannerViewpagerAdpater(List<ImageView> imageViews) {
            this.imageViews = imageViews;
        }

        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView iv = imageViews.get(position);
            container.addView(iv);
            return iv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //super.destroyItem(container, position, object);
            ImageView iv = imageViews.get(position);
            container.removeView(iv);
        }
    }


    public void setStyleItem(int position) {
         switch (position){
             case 0:
                 this.mainviewpager.isScroll = true;
                 this.mainviewpager.setCurrentItem(position, false);
                 this.mainviewpager.isScroll = false;
                 this.saishiTv.setTextColor(getResources().getColor(R.color.default_theme));
                 this.saishiIv.setImageResource(R.mipmap.yanchu_saishi_selected);
                 this.yanchuTv.setTextColor(getResources().getColor(R.color.white));
                 this.yanchuIv.setImageResource(R.mipmap.yanchu_yanchu_noselected);
                 this.jianshenTv.setTextColor(getResources().getColor(R.color.white));
                 this.jianshenIv.setImageResource(R.mipmap.yanchu_jianshen_noselected);
                 this.jiangzuoTv.setTextColor(getResources().getColor(R.color.white));
                 this.jiangzuoIv.setImageResource(R.mipmap.yanchu_jiangzuo_noselected);
                 this.zhanlanTv.setTextColor(getResources().getColor(R.color.white));
                 this.zhanlanIv.setImageResource(R.mipmap.zhanlan_noselected);
                 this.canggeTv.setTextColor(getResources().getColor(R.color.white));
                 this.canggeIv.setImageResource(R.mipmap.cangge_noselected);
                 this.gongyiTv.setTextColor(getResources().getColor(R.color.white));
                 this.gongyiIv.setImageResource(R.mipmap.gongyi_noselected);
                 break;

             case  1:
                 this.mainviewpager.isScroll = true;
                 this.mainviewpager.setCurrentItem(position, false);
                 this.mainviewpager.isScroll = false;

                 this.saishiTv.setTextColor(getResources().getColor(R.color.white));
                 this.saishiIv.setImageResource(R.mipmap.yanchu_saishi_noselected);
                 this.yanchuTv.setTextColor(getResources().getColor(R.color.default_theme));
                 this.yanchuIv.setImageResource(R.mipmap.yanchu_yanchu_selected);
                 this.jianshenTv.setTextColor(getResources().getColor(R.color.white));
                 this.jianshenIv.setImageResource(R.mipmap.yanchu_jianshen_noselected);
                 this.jiangzuoTv.setTextColor(getResources().getColor(R.color.white));
                 this.jiangzuoIv.setImageResource(R.mipmap.yanchu_jiangzuo_noselected);
                 this.zhanlanTv.setTextColor(getResources().getColor(R.color.white));
                 this.zhanlanIv.setImageResource(R.mipmap.zhanlan_noselected);
                 this.canggeTv.setTextColor(getResources().getColor(R.color.white));
                 this.canggeIv.setImageResource(R.mipmap.cangge_noselected);
                 this.gongyiTv.setTextColor(getResources().getColor(R.color.white));
                 this.gongyiIv.setImageResource(R.mipmap.gongyi_noselected);
                 break;
             case  2:
                 this.mainviewpager.isScroll = true;
                 this.mainviewpager.setCurrentItem(position, false);
                 this.mainviewpager.isScroll = false;

                 this.saishiTv.setTextColor(getResources().getColor(R.color.white));
                 this.saishiIv.setImageResource(R.mipmap.yanchu_saishi_noselected);
                 this.yanchuTv.setTextColor(getResources().getColor(R.color.white));
                 this.yanchuIv.setImageResource(R.mipmap.yanchu_yanchu_noselected);
                 this.jianshenTv.setTextColor(getResources().getColor(R.color.default_theme));
                 this.jianshenIv.setImageResource(R.mipmap.yanchu_jianshen_selected);
                 this.jiangzuoTv.setTextColor(getResources().getColor(R.color.white));
                 this.jiangzuoIv.setImageResource(R.mipmap.yanchu_jiangzuo_noselected);
                 this.zhanlanTv.setTextColor(getResources().getColor(R.color.white));
                 this.zhanlanIv.setImageResource(R.mipmap.zhanlan_noselected);
                 this.canggeTv.setTextColor(getResources().getColor(R.color.white));
                 this.canggeIv.setImageResource(R.mipmap.cangge_noselected);
                 this.gongyiTv.setTextColor(getResources().getColor(R.color.white));
                 this.gongyiIv.setImageResource(R.mipmap.gongyi_noselected);
                 break;

             case  3:
                 this.mainviewpager.isScroll = true;
                 this.mainviewpager.setCurrentItem(position, false);
                 this.mainviewpager.isScroll = false;

                 this.saishiTv.setTextColor(getResources().getColor(R.color.white));
                 this.saishiIv.setImageResource(R.mipmap.yanchu_saishi_noselected);
                 this.yanchuTv.setTextColor(getResources().getColor(R.color.white));
                 this.yanchuIv.setImageResource(R.mipmap.yanchu_yanchu_noselected);
                 this.jianshenTv.setTextColor(getResources().getColor(R.color.white));
                 this.jianshenIv.setImageResource(R.mipmap.yanchu_jianshen_noselected);
                 this.jiangzuoTv.setTextColor(getResources().getColor(R.color.default_theme));
                 this.jiangzuoIv.setImageResource(R.mipmap.yanchu_jiangzuo_selected);
                 this.zhanlanTv.setTextColor(getResources().getColor(R.color.white));
                 this.zhanlanIv.setImageResource(R.mipmap.zhanlan_noselected);
                 this.canggeTv.setTextColor(getResources().getColor(R.color.white));
                 this.canggeIv.setImageResource(R.mipmap.cangge_noselected);
                 this.gongyiTv.setTextColor(getResources().getColor(R.color.white));
                 this.gongyiIv.setImageResource(R.mipmap.gongyi_noselected);
                 break;
             case  4:
                 this.mainviewpager.isScroll = true;
                 this.mainviewpager.setCurrentItem(position, false);
                 this.mainviewpager.isScroll = false;


                 this.saishiTv.setTextColor(getResources().getColor(R.color.white));
                 this.saishiIv.setImageResource(R.mipmap.yanchu_saishi_noselected);
                 this.yanchuTv.setTextColor(getResources().getColor(R.color.white));
                 this.yanchuIv.setImageResource(R.mipmap.yanchu_yanchu_noselected);
                 this.jianshenTv.setTextColor(getResources().getColor(R.color.white));
                 this.jianshenIv.setImageResource(R.mipmap.yanchu_jianshen_noselected);
                 this.jiangzuoTv.setTextColor(getResources().getColor(R.color.white));
                 this.jiangzuoIv.setImageResource(R.mipmap.yanchu_jiangzuo_noselected);
                 this.zhanlanTv.setTextColor(getResources().getColor(R.color.default_theme));
                 this.zhanlanIv.setImageResource(R.mipmap.zhanlan_selected);
                 this.canggeTv.setTextColor(getResources().getColor(R.color.white));
                 this.canggeIv.setImageResource(R.mipmap.cangge_noselected);
                 this.gongyiTv.setTextColor(getResources().getColor(R.color.white));
                 this.gongyiIv.setImageResource(R.mipmap.gongyi_noselected);
                 break;
             case  5:
                 this.mainviewpager.isScroll = true;
                 this.mainviewpager.setCurrentItem(position, false);
                 this.mainviewpager.isScroll = false;

                 this.saishiTv.setTextColor(getResources().getColor(R.color.white));
                 this.saishiIv.setImageResource(R.mipmap.yanchu_saishi_noselected);
                 this.yanchuTv.setTextColor(getResources().getColor(R.color.white));
                 this.yanchuIv.setImageResource(R.mipmap.yanchu_yanchu_noselected);
                 this.jianshenTv.setTextColor(getResources().getColor(R.color.white));
                 this.jianshenIv.setImageResource(R.mipmap.yanchu_jianshen_noselected);
                 this.jiangzuoTv.setTextColor(getResources().getColor(R.color.white));
                 this.jiangzuoIv.setImageResource(R.mipmap.yanchu_jiangzuo_noselected);
                 this.zhanlanTv.setTextColor(getResources().getColor(R.color.white));
                 this.zhanlanIv.setImageResource(R.mipmap.zhanlan_noselected);
                 this.canggeTv.setTextColor(getResources().getColor(R.color.default_theme));
                 this.canggeIv.setImageResource(R.mipmap.cangge_selected);
                 this.gongyiTv.setTextColor(getResources().getColor(R.color.white));
                 this.gongyiIv.setImageResource(R.mipmap.gongyi_noselected);
                 break;
             case  6:
                 this.mainviewpager.isScroll = true;
                 this.mainviewpager.setCurrentItem(position, false);
                 this.mainviewpager.isScroll = false;

                 this.saishiTv.setTextColor(getResources().getColor(R.color.white));
                 this.saishiIv.setImageResource(R.mipmap.yanchu_saishi_noselected);
                 this.yanchuTv.setTextColor(getResources().getColor(R.color.white));
                 this.yanchuIv.setImageResource(R.mipmap.yanchu_yanchu_noselected);
                 this.jianshenTv.setTextColor(getResources().getColor(R.color.white));
                 this.jianshenIv.setImageResource(R.mipmap.yanchu_jianshen_noselected);
                 this.jiangzuoTv.setTextColor(getResources().getColor(R.color.white));
                 this.jiangzuoIv.setImageResource(R.mipmap.yanchu_jiangzuo_noselected);
                 this.zhanlanTv.setTextColor(getResources().getColor(R.color.white));
                 this.zhanlanIv.setImageResource(R.mipmap.zhanlan_noselected);
                 this.canggeTv.setTextColor(getResources().getColor(R.color.white));
                 this.canggeIv.setImageResource(R.mipmap.cangge_noselected);
                 this.gongyiTv.setTextColor(getResources().getColor(R.color.default_theme));
                 this.gongyiIv.setImageResource(R.mipmap.gongyi_selected);
                 break;

         }




    }
    @OnClick(value = {R.id.yanchu_saishibt,
            R.id.yanchu_yanchubt,R.id.yanchu_jianshenbt,R.id.yanchu_jiangzuobt,
            R.id.yanchu_zhanlanbt,R.id.yanchu_canggebt,R.id.yanchu_gongyibt})
    public void onMyClick(View view){
         switch (view.getId()){
             case R.id.yanchu_saishibt:
                 setStyleItem(0);
                 break;
             case R.id.yanchu_yanchubt:
                 setStyleItem(1);
                 break;
             case R.id.yanchu_jianshenbt:
                 setStyleItem(2);
                 break;
             case R.id.yanchu_jiangzuobt:
                 setStyleItem(3);
                 break;
             case R.id.yanchu_zhanlanbt:
                 setStyleItem(4);
                 break;
             case R.id.yanchu_canggebt:
                 setStyleItem(5);
                 break;
             case R.id.yanchu_gongyibt:
                 setStyleItem(6);
                 break;
         }
    }
    public class MainViewPagerAdapter extends FragmentPagerAdapter
    {
        public List<BaseMainFragment> list;

        public MainViewPagerAdapter(FragmentManager fm,List<BaseMainFragment> list) {
            super(fm);
            this.list=list;
        }

        public int getCount()
        {
            return this.list.size();
        }

        public Fragment getItem(int paramInt)
        {
            return (Fragment)this.list.get(paramInt);
        }
    }

}
